import { debounce } from "../../utils/hocks";
import {getIconBalance} from "../../api/modules/payment"
import { fetchOrderPlayletPreview } from "../../api/modules/trade";
// /Users/wanghongsheng/工作/miniprograms/ ShortVideo/components/unlock-selector-dialog/unlock-selector-dialog.js
Component({
  data: {
    needLoading: false,
    unlockMethods: [],
    balance:{},
    current: -1
  },
  properties: {
    currentVideo:{},
    needUnlock: {
      type: Boolean,
      default: false,
      observer(val) {
        if (val) {
          this.fetchUnlockMethods()
          this.fetchCoinBalance()
        }
      }
    },
  },
  methods: {
   async fetchCoinBalance(){
      const {data} =await getIconBalance(null,{loading:false});
      this.setData({
        balance:data
      })
    },
    toggleMethod(e) {
      let idx = e.currentTarget.dataset.idx;
      if (this.data.current === idx) {
        return;
      }
      this.setData({
        current: idx
      })
    },
   async fetchUnlockMethods() {
      this.setData({
        needLoading: true
      })
      // console.log(this.properties)
      const {data}=await fetchOrderPlayletPreview({
        id:this.properties.currentVideo.id
      },{loading:false})
      let unlockMethods=data.playletSku.map(v=>{
        return {
          previewCode: data.previewCode,
          skuId: v.skuId,
          goodsId: v.goodsId,
          label:
            v.unlockEpisodeEnd === v.unlockEpisodeStart
              ? `解锁第${v.unlockEpisodeStart}集`
              : `解锁第${v.unlockEpisodeStart}集至第${v.unlockEpisodeEnd}集`,
          hf: `${v.realPrice}`,
          // paymentModeId: 0
        }
      })

        this.setData({
          current:0,
          needLoading: false,
          unlockMethods: unlockMethods
        })
    },
    maskHandler() {
      console.log()
      this.triggerEvent("hide")
    },
    handleSubmit:debounce(function(){
      let target = this.data.unlockMethods[this.data.current];
      this.triggerEvent("selected", target);
    },200),
    cancel() {
      this.triggerEvent('hide');
    }
  }
})